package com.atguigu.chapter11;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/6/19 9:26
 */
public class Flink09_Table_Window_Over_SQL {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        tEnv.executeSql("create table sensor(" +
                            "   id string, " +
                            "   ts bigint," +
                            "   vc int, " +
                            "   et as to_timestamp(from_unixtime(ts/1000)), " +  // 根据long型的数据计算出来一个时间戳类型, 用来计算水印
                            "   watermark for et AS et - interval '3' second " +  // 生成水印
                            ")with(" +
                            "   'connector' = 'filesystem'," +
                            "   'path' = 'input/sensor.txt', " +
                            "   'format' = 'csv'" +
                            ")");
        
        tEnv
            .sqlQuery("select" +
                          " id, " +
                          " ts," +
                          " vc," +
                          " et," +
                          //" sum(vc) over(partition by id order by et rows between unbounded preceding and current row) vc_sum, " +
                          //" max(vc) over(partition by id order by et rows between unbounded preceding and current row) vc_sum " +
//                          " sum(vc) over(partition by id order by et rows between 1 preceding and current row) vc_sum " +
//                          " sum(vc) over(partition by id order by et range between unbounded preceding and current row) vc_sum " +
//                          " sum(vc) over(partition by id order by et range between interval '1' second preceding and current row) vc_sum " +
                          " sum(vc) over(partition by id order by et rows between current row and 1 following) vc_sum " +  // 不支持
                          "from sensor")
            .execute()
            .print();
        
    }
}
